
//![[CC
import QWEventDispatcher from "../GMDBase/QWEventDispatcher"
import QWEvent from "../GMDBase/QWEvent"  
import { gcfg } from "../../gcfg"; 
import AccountEvts from "../GMDLogin/AccountEvts";
import CCWechatNative from "./CCWechatNative";
//!]]
/*[*/export default/*]*/ class WechatAccount
{
    public static userInfo:any = {}

    /**
     * 微信客户端得到的用户登录凭证
     * 获得该凭证后 wxAccount 会一直持有直到重新登录更新，但该凭证的实际有效期是 5 分钟。
     */
    public static code:string = ''

    public static Go(){
        CCWechatNative.Go(gcfg.AppId,gcfg.UniversalLink)
    }


    public static addEventListener(type: string, listener: Function, thisObject: any): void{
        this.Events.addEventListener(type,listener,thisObject)
    }

    static get Events():QWEventDispatcher{  
        if(!this.m_Events)
        this.m_Events = new QWEventDispatcher()
        return this.m_Events
    }
    static m_Events:QWEventDispatcher = null
 
    //生成NS登录包
    static GenerateNSLoginNM()
    {
        console.log("LGJS GenerateNSLoginNM")
        var nm = {
            n: 'ck',
            tp: 'wxg',
            wxg: {
                a: gcfg["ProjectID"],
                c: this.code
            }
        }
        return nm
    }

    // 启动登录过程
    public static login(obj:any) { 
        console.log("LGJS login")

        if(!cc.sys.isNative) //非原生环境返回成功
        {
            console.log("LGJS dev login success")
            //{code:string}
            this.code = "xxx";
            var evt =  new QWEvent(AccountEvts.OnLoginThirdPartySuccess)
            evt.details = "xxx"
            this.Events.dispatchEvent(evt)
            
            return
        }

        CCWechatNative.Login({
            success:(res)=>{
                console.log("LGJS login success")
                //{code:string}
                this.code = res.code;
                var evt =  new QWEvent(AccountEvts.OnLoginThirdPartySuccess)
                evt.details = res.code
                this.Events.dispatchEvent(evt)
            },
            fail:(res)=>{
                console.log("LGJS login fail",res.errCode)
                //{errCode:number}
                this.Events.dispatchEvent(new QWEvent(AccountEvts.OnLoginThirdPartyFail))
            }
        })
    } 
};
    
 
  